package com.macro.mall.portal.service.impl;

import com.macro.mall.mgb.mapper.ProductMapper;
import com.macro.mall.mgb.model.Member;
import com.macro.mall.mgb.model.Product;
import com.macro.mall.portal.domain.MemberProductCollection;
import com.macro.mall.portal.repository.MemberProductCollectionRepository;
import com.macro.mall.portal.service.MemberCollectionService;
import com.macro.mall.portal.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

/**
 * @author wangwang
 * @version 1.0
 * @description 会员商品收藏管理
 * @date 2025/7/31 18:57
 */
@Service
public class MemberCollectionServiceImpl implements MemberCollectionService {
    @Autowired
    private MemberProductCollectionRepository memberProductCollectionRepository;

    @Autowired
    private MemberService memberService;

    @Autowired
    private ProductMapper productMapper;

    @Value("${mongo.insert.sqlEnable}")
    private Boolean sqlEnable;

    @Override
    public int add(MemberProductCollection productCollection) {
        int count = 0;
        if(productCollection.getProductId() == null){
            return 0;
        }

        Member member = memberService.getCurrentMember();
        productCollection.setMemberId(member.getId());
        productCollection.setMemberNickname(member.getNickname());
        productCollection.setMemberIcon(member.getIcon());

        MemberProductCollection memberProductCollection = memberProductCollectionRepository.findByMemberIdAndProductId(productCollection.getMemberId(),productCollection.getProductId());
        if(memberProductCollection == null){
            if (sqlEnable) {
                Product product = productMapper.selectById(productCollection.getProductId());
                if (product == null || product.getDeleteStatus() == 1) {
                    return 0;
                }
                productCollection.setProductName(product.getName());
                productCollection.setProductSubTitle(product.getSubTitle());
                productCollection.setProductPrice(product.getPrice() + "");
                productCollection.setProductPic(product.getPic());
            }
            memberProductCollectionRepository.save(productCollection);
            count = 1;
        }
        return count;
    }

    @Override
    public int delete(Long productId) {

        Member member = memberService.getCurrentMember();
        return memberProductCollectionRepository.deleteByMemberIdAndProductId(member.getId(), productId);
    }

    @Override
    public Page<MemberProductCollection> list(Integer pageNum, Integer pageSize) {

        Member member = memberService.getCurrentMember();
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        return memberProductCollectionRepository.findByMemberId(member.getId(), pageable);
    }

    @Override
    public MemberProductCollection detail(Long productId) {

        Member member = memberService.getCurrentMember();
        return memberProductCollectionRepository.findByMemberIdAndProductId(member.getId(), productId);
    }

    @Override
    public void clear() {
        Member member = memberService.getCurrentMember();
        memberProductCollectionRepository.deleteAllByMemberId(member.getId());
    }
}
